package HW.MyStatisticsClass;

/**************************************************************
* Class MyStatisticsClass
* @author Jeff Thomas
* @vesion 1.0.0
* Date: 02.06.2008
* Copyright 2008, Jeff Thomas
***************************************************************/

import java.util.ArrayList;

class MyStatisticsClass<T extends Number>
{
	public static void main(String[] args)
	{
		//sample data 1
		ArrayList<Double> numbers = new ArrayList<Double>();
		numbers.add(1.0);
		numbers.add(2.1);
		numbers.add(3.6);
		numbers.add(4.5);
		numbers.add(8.2);
		
		//sample data 2
		ArrayList<Integer> numbers2 = new ArrayList<Integer>();
		numbers2.add(1);
		numbers2.add(6);
		numbers2.add(3);
		numbers2.add(8);
		numbers2.add(2);
		
		Double result = standardDeviation(numbers);
		Double result2 = standardDeviation(numbers2);
		System.out.printf("%.2f", result);
		System.out.printf("\n%.2f", result2);
	}
/**************************************************************
* Standard Deviation
* @param a - Generic type T ArrayList of numbers
* @return - Double of standard deviation
* Task:	1. Calculate the standard deviation for an ArrayList
* Side Effects: None
***************************************************************/
	public static <T extends Number> double standardDeviation(ArrayList<T> a)
	{
		//determine average of array
		double average = calcAverage(a);
		double top = 0.0;
		//calculate the standard deviation
		for (int i = 0; i < a.size(); i++)
		{
			//(n - average)^2
			top = top + Math.pow(a.get(i).doubleValue() - average, 2);
		}
		return Math.sqrt(top / average);
	}
/**************************************************************
* Calculate Average
* @param a - Generic type T ArrayList of numbers
* @return - Average of numbers in an ArrayList
* Task: 	1. Calculate the average of the numbers in the Array
* Side Effects: None
***************************************************************/
	private static <T extends Number> double calcAverage(ArrayList<T> a)
	{
		double sum = 0.0;
		for (int i = 0; i < a.size(); i++)
		{
			//average is sum of all numbers / total numbers
			sum = sum + a.get(i).doubleValue();
		}
		return sum / a.size();
	}
}
/**************************************************************
* I, Jeff Thomas, certify that each part of this program was
* written by me, except as noted within the code.
*
*						_________________________
***************************************************************/